﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using HYFamilyLibManager.FamilyLibModule;

namespace DBService
{
	public partial class DlgAddUser : System.Windows.Forms.Form
	{
		public DlgAddUser(DlgServerManager main, string mGroup)
		{
			this.InitializeComponent();
			this.mGroup = mGroup;
			this.mainForm = main;
			this.parent = this.mainForm.CurrentDepartment;
		}

		private void DlgAddDepartment_Load(object sender, EventArgs e)
		{
			this.txtParent.Text = this.mainForm.CurrentDepartment.Name;
			foreach (DbRight dbRight in DbRight.GetAllRights(BimServerDB.Default))
			{
				if (dbRight.Name != "系统管理")
				{
					this.lbxRight.Items.Add(dbRight);
				}
			}
			foreach (DbRole item in DbRole.GetAllRoles(BimServerDB.Default))
			{
				this.lbxRole.Items.Add(item);
			}
		}

		private bool IsValidEmail(string strEmail)
		{
			return Regex.IsMatch(strEmail, "^[\\w\\.]+([-]\\w+)*@[A-Za-z0-9-_]+[\\.][A-Za-z0-9-_]");
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.txtName.Text.Trim() == "" || this.txtPassword.Text.Trim() == "")
				{
					MessageBox.Show("请填写用户名和密码。");
				}
				else
				{
					using (List<DbUser>.Enumerator enumerator = DbUser.GetAllUsers(BimServerDB.Default).GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							if (enumerator.Current.Name == this.txtName.Text.Trim())
							{
								MessageBox.Show("该用户名已存在。");
								this.txtName.Focus();
								return;
							}
						}
					}
					this.User = new DbUser(this.txtName.Text.Trim(), this.txtEmail.Text.Trim(), DbUser.ToMd5(this.txtPassword.Text.Trim()), this.txtChineseName.Text.Trim());
					this.User.Remark = this.txtRemark.Text;
					this.User.AddToDb(BimServerDB.Default);
					this.User.ToDepartment(this.mainForm.currentDepartment.Id);
					foreach (object obj in this.lbxRight.SelectedItems)
					{
						this.User.AddRight((DbRight)obj);
					}
					List<DbRole> list = new List<DbRole>();
					foreach (object obj2 in this.lbxRole.SelectedItems)
					{
						DbRole item = (DbRole)obj2;
						list.Add(item);
					}
					this.User.UpdateRole(list, this.mGroup);
					base.DialogResult = DialogResult.OK;
					base.Close();
				}
			}
			catch (Exception)
			{
				this.txtName.Text = "";
				MessageBox.Show("注册失败，该用户名已存在！");
			}
		}

		private void lbxRole_SelectedIndexChanged(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxRight.Items.Count; i++)
			{
				this.lbxRight.SetSelected(i, false);
			}
			foreach (object obj in this.lbxRole.SelectedIndices)
			{
				int index = (int)obj;
				foreach (DbRight dbRight in (this.lbxRole.Items[index] as DbRole).GetRights())
				{
					for (int j = 0; j < this.lbxRight.Items.Count; j++)
					{
						if (((DbRight)this.lbxRight.Items[j]).Name == dbRight.Name)
						{
							this.lbxRight.SetSelected(j, true);
							break;
						}
					}
				}
			}
		}

		private DlgServerManager mainForm;

		private DbDepartment parent;

		public DbUser User;

		private string mGroup;
	}
}
